package dwz.web.management;

import java.util.List;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.wx.core.service.CoreService;
import com.wx.core.service.MenuService;
import com.wx.util.WeixinUtil;

import dwz.business.menu.Menu;
import dwz.business.menu.MenuEvent;
import dwz.business.menu.MenuLevel;
import dwz.business.menu.MenuServiceMgr;
import dwz.business.menu.MenuStatus;
import dwz.framework.sys.exception.ServiceException;
import dwz.persistence.BaseConditionVO;
import dwz.web.BaseController;

@Controller("management.menuController")
@RequestMapping(value="/management/menu")
public class MenuController extends BaseController{
	private static Logger log = LoggerFactory.getLogger(MenuController.class);
	@Resource
	private MenuServiceMgr menuMgr;
	@Resource
	private MenuService menuService;

		

	/**
	 * 
	 * @Method: list 
	 * @Description:  [查询一级菜单列表]  
	 * @Author:       [liaoxj]     
	 * @CreateDate:   [2014-9-5 上午11:55:15]   
	 * @UpdateUser:   [liaoxj]     
	 * @UpdateDate:   [2014-9-5 上午11:55:15]   
	 * @UpdateRemark: [说明本次修改内容]  
	 * @param vo
	 * @param model
	 * @return String
	 */
	@RequestMapping("")
	public String list(BaseConditionVO vo, Model model) {
		vo.setLevel(MenuLevel.FIRST.toString());
		List<Menu> userList = menuMgr.searchMenu(vo);
		Integer totalCount = menuMgr.searchUserNum(vo);
		
		model.addAttribute("userList", userList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("menuStatusList", MenuStatus.values());
		model.addAttribute("pageSize", vo.getPageSize());

		return "/management/menu/list";
	}
	
	/**
	 * 
	 * @Method: secondlist 
	 * @Description:  [查询二级菜单列表]  
	 * @Author:       [liaoxj]     
	 * @CreateDate:   [2014-9-5 上午11:55:37]   
	 * @UpdateUser:   [liaoxj]     
	 * @UpdateDate:   [2014-9-5 上午11:55:37]   
	 * @UpdateRemark: [说明本次修改内容]  
	 * @param vo
	 * @param model
	 * @return 
	 * @return String
	 */
	@RequestMapping("/secondlist")
	public String secondlist(BaseConditionVO vo, Model model) {
		vo.setLevel(MenuLevel.SECOND.toString());
		List<Menu> userList = menuMgr.searchMenu(vo);
		Integer totalCount = menuMgr.searchUserNum(vo);
		
		model.addAttribute("userList", userList);
		model.addAttribute("totalCount", totalCount);
		model.addAttribute("menuStatusList", MenuStatus.values());
		model.addAttribute("pageSize", vo.getPageSize());

		//查询所有的一级菜单名称
		vo.setLevel(MenuLevel.FIRST.toString());
		List<Menu> fmenuList = menuMgr.searchMenu(vo);
		model.addAttribute("fmenuList", fmenuList);
		
		return "/management/menu/second_list";
	}
	
	/**
	 * 
	 * @Method: add 
	 * @Description:  [跳转到一级菜单信息页面]  
	 * @Author:       [liaoxj]     
	 * @CreateDate:   [2014-9-5 上午11:55:58]   
	 * @UpdateUser:   [liaoxj]     
	 * @UpdateDate:   [2014-9-5 上午11:55:58]   
	 * @UpdateRemark: [说明本次修改内容]  
	 * @param model
	 * @return String
	 */
	@RequestMapping("/add")
	public String add(Model model) {
		model.addAttribute("navTabId", "menuLiNav");
		return "/management/menu/add";
	}
	
	/**
	 * 
	 * @Method: secondAdd 
	 * @Description:  [跳转到二级菜单新增页面]  
	 * @Author:       [liaoxj]     
	 * @CreateDate:   [2014-9-5 上午11:56:19]   
	 * @UpdateUser:   [liaoxj]     
	 * @UpdateDate:   [2014-9-5 上午11:56:19]   
	 * @UpdateRemark: [说明本次修改内容]  
	 * @param model
	 * @return String
	 */
	@RequestMapping("/secondAdd")
	public String secondAdd(Model model) {
		//查询所有的一级菜单信息
		BaseConditionVO vo=new BaseConditionVO();
		vo.setLevel(MenuLevel.FIRST.toString());
		List<Menu> menuList = menuMgr.searchMenu(vo);
		model.addAttribute("menuList", menuList);
		model.addAttribute("menuEventList", MenuEvent.values());
		
		return "/management/menu/second_add";
	}
	
	/**
	 * 
	 * @Method: edit 
	 * @Description:  [跳转到一级菜单编辑页面]  
	 * @Author:       [liaoxj]     
	 * @CreateDate:   [2014-9-5 上午11:56:50]   
	 * @UpdateUser:   [liaoxj]     
	 * @UpdateDate:   [2014-9-5 上午11:56:50]   
	 * @UpdateRemark: [说明本次修改内容]  
	 * @param menuId  菜单编号
	 * @param model
	 * @return String
	 */
	@RequestMapping("/edit/{menuId}")
	public String edit(@PathVariable("menuId") int menuId, Model model) {
		Menu menu = menuMgr.getMenu(menuId);
		model.addAttribute("vo", menu);
		return "/management/menu/edit";
	}
	
	/**
	 * 
	 * @Method: secondEdit 
	 * @Description:  [跳转到二级菜单编辑页面]  
	 * @Author:       [liaoxj]     
	 * @CreateDate:   [2014-9-5 上午11:57:11]   
	 * @UpdateUser:   [liaoxj]     
	 * @UpdateDate:   [2014-9-5 上午11:57:11]   
	 * @UpdateRemark: [说明本次修改内容]  
	 * @param menuId  菜单编号
	 * @param model
	 * @return String
	 */
	@RequestMapping("/secondEdit/{menuId}")
	public String secondEdit(@PathVariable("menuId") int menuId, Model model) {
		Menu menu = menuMgr.getMenu(menuId);
		model.addAttribute("vo", menu);
		
		//查询所有的一级菜单信息
		BaseConditionVO vo=new BaseConditionVO();
		vo.setLevel(MenuLevel.FIRST.toString());
		List<Menu> menuList = menuMgr.searchMenu(vo);
		model.addAttribute("menuList", menuList);
		model.addAttribute("menuEventList", MenuEvent.values());
		return "/management/menu/second_edit";
	}
	
	/**
	 * 
	 * @Method: insert 
	 * @Description:  [新增一级菜单]  
	 * @Author:       [liaoxj]     
	 * @CreateDate:   [2014-9-5 上午11:57:53]   
	 * @UpdateUser:   [liaoxj]     
	 * @UpdateDate:   [2014-9-5 上午11:57:53]   
	 * @UpdateRemark: [说明本次修改内容]  
	 * @param menu   菜单实体
	 * @return ModelAndView
	 */
	@RequestMapping(value = "/insert", method = RequestMethod.POST)
	public ModelAndView insert(Menu menu) {
		try {
			menuMgr.addMenu(menu);
		} catch (ServiceException e) {
			return ajaxDoneError(e.getMessage());
		}
//		return ajaxDone(200, getMessage("msg.operation.success"), "", navTabId,"closeCurrent");
		return ajaxDoneSuccess(getMessage("msg.operation.success"));
	}
	
    /**
     * 
     * @Method: insertSecond 
     * @Description:  [保存二级菜单信息]  
     * @Author:       [liaoxj]     
     * @CreateDate:   [2014-9-5 上午11:59:34]   
     * @UpdateUser:   [liaoxj]     
     * @UpdateDate:   [2014-9-5 上午11:59:34]   
     * @UpdateRemark: [说明本次修改内容]  
     * @param menu 菜单实体
     * @return ModelAndView
     */
	@RequestMapping(value = "/insertSecond", method = RequestMethod.POST)
	public ModelAndView insertSecond(Menu menu) {
		try {
			//验证二级菜单的关键字是否存在
			Boolean isFlag=menuMgr.isExistMenuKey(menu.getWechatMenu().getKeyName());
			if(isFlag){
				return ajaxDoneError(getMessage("msg.operation.failure"));
			}
			
			//保存二级菜单信息
			menuMgr.addSecondMenu(menu);
		} catch (ServiceException e) {
			return ajaxDoneError(e.getMessage());
		}

		return ajaxDoneSuccess(getMessage("msg.operation.success"));
	}
	
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public ModelAndView update(Menu menu) {
		menuMgr.updateMenu(menu);
		return ajaxDoneSuccess(getMessage("msg.operation.success"));
	}
	
	@RequestMapping("/delete/{menuId}")
	public ModelAndView delete(@PathVariable("menuId") int menuId) {
		menuMgr.updateMenuStatus(menuId,MenuStatus.DELETED.toString());
		return ajaxDoneSuccess(getMessage("msg.operation.success"));
	}
	
	@RequestMapping("/active/{menuId}")
	public ModelAndView active(@PathVariable("menuId") int menuId) {
		menuMgr.updateMenuStatus(menuId,MenuStatus.ACTIVE.toString());
		return ajaxDoneSuccess(getMessage("msg.operation.success"));
	}
	
	@RequestMapping("/inactive/{menuId}")
	public ModelAndView inactive(@PathVariable("menuId") int menuId) {
		menuMgr.updateMenuStatus(menuId,MenuStatus.INACTIVE.toString());
		return ajaxDoneSuccess(getMessage("msg.operation.success"));
	}
	
	
	@RequestMapping("/createMenu")
	public ModelAndView createMenu(Model model) {
		log.info("进入=======MenuController.createMenu====");
		String appId= getMessage("wx.app.id");
		String appSecret= getMessage("wx.app.secret");
		Integer result=menuService.createMenu(appId, appSecret);
		log.info("MenuController.createMenu.result=="+result);
		if(result!=null&&result==0){
			return ajaxDoneSuccess(getMessage("msg.operation.success"));
		}else{
			return ajaxDoneError(getMessage("msg.operation.failure")+":"+result);
		}
	}
	
	@RequestMapping("/deleteMenu")
	public ModelAndView deleteMenu(Model model) {
		String appId= getMessage("wx.app.id");//
		String appSecret= getMessage("wx.app.secret");//
		Integer result=menuService.deleteMenu(appId, appSecret);
		if(result!=null&&result==0){
			return ajaxDoneSuccess(getMessage("msg.operation.success"));
		}else{
			return ajaxDoneError(getMessage("msg.operation.failure")+":"+result);
		}
	}
	
	
}
